package com.nifostasky.pattern.producer;

import com.nifostasky.pattern.config.RabbitTopicConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 主题模式:
 *
 * 首先分析：通配符模式其实就是将 路由模式中的 direct Exchange 换成了 topic Exchange， 使其不仅可以将exchange和queue以routing key全匹配的方式进行绑定，而且还支持通配符。
 *
 * routingkey 一般都是有一个或多个单词组成，多个单词之间以”.”分割，例如： item.insert
 *
 * 通配符规则：
 *
 * #：匹配一个或多个单词
 *
 * *：匹配一个单词
 *
 * 举例：
 *
 * item.#：能够匹配item.insert.abc 或者 item.insert
 *
 * item.*：只能匹配item.insert
 */
@Component
public class TopicProducerPattern {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendWithTopic() {
        //队列1接受,队列2不接受
        rabbitTemplate.convertAndSend(RabbitTopicConfig.EXCHANGE_NAME, "insert.user.log", "topic mq hello~~~ routing is insert.user.log");
        //队列1接受,队列2接受
        rabbitTemplate.convertAndSend(RabbitTopicConfig.EXCHANGE_NAME, "update.user", "topic mq hello~~~ routing is update.user");
        //队列1不接受,队列2不接受
        rabbitTemplate.convertAndSend(RabbitTopicConfig.EXCHANGE_NAME, "update.user.log", "topic mq hello~~~ routing is update.user.log");
        //队列2接受,队列1不接受
        rabbitTemplate.convertAndSend(RabbitTopicConfig.EXCHANGE_NAME, "update.delete", "topic mq hello~~~routing is update.delete");
        //队列2不接受,队列1不接受
        rabbitTemplate.convertAndSend(RabbitTopicConfig.EXCHANGE_NAME, "delete.user.log", "topic mq hello~~~routing is delete.user.log");
    }
}
